<template>
  <!-- 路由图形化展示 -->
  <div class="link-view-box">
    <div class="line-item flex pt-10 pb-10">
      <div
        v-for="(station, stationIndex) in stationList"
        :key="stationIndex"
        class="flex align-center"
      >
        <!-- 站点间光缆 -->
        <div v-if="stationIndex !== 0" class="cable-box flex-column align-center p-10 pt-40">
          <span v-if="station.hasCable" class="no-warp">
            {{ station.cable.cableName }}
          </span>
          <el-divider content-position="center">
            <span class="text-ellipsis">{{ channelCount || '' }}</span>
          </el-divider>
        </div>
        <!-- 站点 -->
        <div class="flex-column align-center">
          <span class="no-warp">{{ getRegionName(station.stationId) }}</span>
          <div class="station-box mt-10">
            <div
              v-for="(node, index) in station.nodeList"
              :key="node.nodeId + '_' + index"
              class="flex align-center"
            >
              <!-- 站点内光缆 -->
              <div
                v-if="index !== 0"
                class="cable-box flex-column align-center"
                :class="!node.hasCable ? 'p-10' : 'pr-10 pl-10'"
              >
                <span v-if="node.hasCable" class="no-warp">
                  {{ node.cable.cableName }}
                </span>
                <el-divider content-position="center">
                  <span class="text-ellipsis">{{ channelCount || '' }}</span>
                </el-divider>
              </div>
              <!-- 节点图标 -->
              <div class="flex-column align-center">
                <i class="iconfont mb-10" :class="'icon-AFS' + node.nodeType.toLowerCase()" />
                <span class="device-name">{{ node.nodeName }}</span>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
  import { mapGetters } from 'vuex'
  import { getParentsById } from '@/utils/common'

  export default {
    props: {
      segmentList: {
        type: Array,
        default: () => []
      },
      channelCount: {
        type: Number,
        default: 0
      }
    },
    data() {
      return {
        stationList: []
      }
    },
    computed: {
      ...mapGetters(['regionTree'])
    },
    mounted() {
      this.formatData()
    },
    methods: {
      getRegionName(regionId) {
        const regionList = (getParentsById(this.regionTree, regionId, 'regionId') || []).reverse()
        return regionList.map(i => i.regionName).join('/')
      },
      formatData() {
        const stationList = []
        console.log('this.segmentList', this.segmentList)
        this.segmentList &&
          this.segmentList.forEach(
            ({ startNode, endNode, cable, fiber, startStationId, endStationId }, index) => {
              // 如果起始节点或结束节点不存在，则跳过本次循环
              if (!startNode || !endNode) return

              // 如果是第一个节点，则将起始节点添加到站点列表中
              if (index === 0 && startNode.stationId) {
                stationList.push({
                  stationId: startNode.stationId,
                  nodeList: [startNode]
                })
              }

              // 获取最后一个站点的信息
              const lastStation = stationList[stationList.length - 1] || {}
              const stationData = lastStation.stationId === endNode.stationId ? lastStation : null
              const cableData = cable || fiber

              // 如果当前节点属于最后一个站点，则将结束节点添加到该站点中
              if (stationData) {
                const nodeItem = { ...endNode }
                if (cableData && startStationId === endStationId) {
                  nodeItem.hasCable = true
                  nodeItem.cable = cableData
                }
                stationData.nodeList.push(nodeItem)
              } else {
                // 否则，将结束节点添加到新的站点中
                const stationItem = {
                  cable: cableData,
                  stationId: endNode.stationId,
                  nodeList: [endNode]
                }
                if (cableData && startStationId !== endStationId) {
                  stationItem.hasCable = true
                  stationItem.cable = cableData
                }
                stationList.push(stationItem)
              }
            }
          )
        this.stationList = stationList
        console.log('stationList', stationList)
      }
    }
  }
</script>

<style lang="scss" scoped>
  @import '@/assets/css/mixin.module.scss';

  .link-view-box {
    .line-item {
      overflow-y: auto;
    }

    ::v-deep .el-divider--horizontal {
      margin: 30px 0;
    }

    .station-box {
      display: flex;
      padding: 20px;
      justify-content: center;

      @include themeify {
        border: 2px dashed themed('dividerColor');
      }

      .iconfont {
        font-size: 60px;
        color: #00aeff;
      }

      .no-warp ~ ::v-deep .el-divider--horizontal {
        margin: 19px 0;
      }
    }

    .cable-box {
      min-width: 150px;
    }

    .node-item {
      margin: 0 10px;
      width: 80px;
    }

    .device-name {
      width: 90px;
      text-align: center;
      font-size: 12px;
    }
  }
</style>
